package cn.itcast.service.impl;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.mapper.AssessmentMapper;
import cn.itcast.mapper.MentalityMapper;
import cn.itcast.mapper.SunAssessMapper;
import cn.itcast.pojo.Assessment;
import cn.itcast.service.AssessmentService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = AssessmentService.class)
@Transactional
public class AssessmentServiceImpl implements AssessmentService {
    @Autowired
    AssessmentMapper assessmentMapper;
    @Autowired
    MentalityMapper mentalityMapper;
    @Autowired
    SunAssessMapper sunAssessMapper;

    @Override
    public PageResult findPage(QueryPageBean bean) {
        PageHelper.startPage(bean.getCurrentPage(), bean.getPageSize());
        List<Map<String, Object>> list = assessmentMapper.findPage(bean.getQueryString());
        for (Map<String, Object> map : list) {
            if (map.get("assessDate") != null) {
                map.put("assessDate", new SimpleDateFormat("yyyy-MM-dd").format(map.get("assessDate")));
            }
            if (map.get("sun_Sum") != null) {
                Integer sunSum = (Integer) map.get("sun_Sum");
                if (sunSum * 1.0 < 28 * 0.75) {
                    map.put("sun_Sum", "否");
                } else {
                    map.put("sun_Sum", "是");
                }
            }
            if (map.get("mentality_Sum") != null) {
                Integer mentality_sum = (Integer) map.get("mentality_Sum");
                if (mentality_sum * 1.0 < 40 * 0.75) {
                    map.put("mentality_Sum", "否");
                } else {
                    map.put("mentality_Sum", "是");
                }
            }


        }
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(list);
        return new PageResult(mapPageInfo.getTotal(), list);
    }

    @Override
    public void add(String fileNumber, Map sun, Map mentality) {
        Assessment assessment = assessmentMapper.findByFileNumber(fileNumber);
        //获取阳虚总分数;
        Integer sunSum = 0;
        Set set = sun.keySet();
        for (Object o : set) {
            Integer value = Integer.parseInt((String) sun.get(o));
            sunSum += value;
        }
        //获取精神测评分数
        int mentality_Sum = 0;
        Set set1 = mentality.keySet();
        for (Object o : set1) {
            Integer value = Integer.parseInt((String) mentality.get(o));
            mentality_Sum += value;
        }

        //如果没有查询到这个报告表  那就创建一个报告单
        if (assessment == null) {
            assessment = new Assessment();
            assessment.setFileNumber(fileNumber);
            assessment.setAssessDate(DateUtils.getToday());
            assessment.setSun_Sum(sunSum);
            assessment.setMentality_Sum(mentality_Sum);
            assessmentMapper.add(assessment);
            //给两个子表添加数据;
            Integer h_id = assessment.getId();
            sun.put("h_id", h_id);
            sunAssessMapper.add(sun);
            mentality.put("h_id", h_id);
            mentalityMapper.add(mentality);
        } else {
            //如果查询到了报告单,就修改报告单
            assessment.setAssessDate(DateUtils.getToday());
            assessment.setSun_Sum(sunSum);
            assessment.setMentality_Sum(mentality_Sum);
            assessmentMapper.updateAdd(assessment);
            //给两个子表修改数据;
            Integer h_id = assessment.getId();
            sun.put("h_id", h_id);
            sunAssessMapper.updateAdd(sun);
            mentality.put("h_id", h_id);
            mentalityMapper.updateAdd(mentality);
        }


    }

    @Override
    public List<Map<String, Object>> findAllByfileNumber(String fileNumber) {
        Map<String, Object> sunMap = sunAssessMapper.findByfileNumber(fileNumber);
        Map<String, Object> menMap = mentalityMapper.findByfileNumber(fileNumber);
        List<Map<String, Object>> list = new ArrayList<>();
        list.add(sunMap);
        list.add(menMap);
        for (Map<String, Object> map : list) {
            Set<String> set = map.keySet();
            for (String key : set) {
                Integer value = (Integer) map.get(key);
                if (value == 0) {
                    map.put(key, "没有(根本不)");
                } else if (value == 1) {
                    map.put(key, "很少(有一点)");
                } else if (value == 2) {
                    map.put(key, "有时(有些)");
                } else if (value == 3) {
                    map.put(key, "经常(相当)");
                } else if (value == 4) {
                    map.put(key, "总是(非常)");
                } else {
                    map.put(key, "");
                }
            }
        }
        return list;
    }

    @Override
    public void deleteByFileNumber(String fileNumber) {
        //第一个删除两个子表的数据.这里直接使用多查询;了;
        sunAssessMapper.deleteByFileNumber(fileNumber);
        mentalityMapper.deleteByFileNumber(fileNumber);
        //删除用户的体检表. 要删除三个地方,  第一个是主表,直接将主表的  体检日期,和两个子项的分数设置为null就可以了;
        assessmentMapper.deleteByFileNumber(fileNumber);
    }

    @Override
    public List<Map<String,Object>> findAllAndName() {
       return assessmentMapper.findAllAndName();
    }

    //根据会员id查询对应的评测结果
    public Assessment findByMemberId(Integer id) {
        return assessmentMapper.findByMemberId(id);
    }
}
